package com.allenlee.mvphelper.module.login;

import com.allenlee.mvphelper.base.BasePresenter;
import com.allenlee.mvphelper.base.IBaseView;
import com.allenlee.mvphelper.base.bean.HttpResult;
import com.allenlee.mvphelper.net.retrofit.RxScheduler;
import com.allenlee.mvphelper.net.retrofit.exception.MHConsumer;

import java.util.HashMap;

import io.reactivex.functions.Consumer;

/**
 * @author : lifei
 * 创建时间： 19-11-27
 * 更改时间： 19-11-27
 * Email : allen.lee723@qq.com
 * @version :
 * Description：
 */
public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.Presenter {

    private LoginContract.Model model;

    @Override
    public void attach(IBaseView view) {
        super.attach(view);
        model = new LoginModel();
    }

    @Override
    public void login() {
        mView.showLoading("登录中...");

        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put(LoginContract.USER_NAME, mView.getClientId());
        paramMap.put(LoginContract.PASSWORD, mView.getPassword());
        model.login(paramMap).compose(RxScheduler.Flo_io_main())
            .as(mView.bindAutoDispose())
            .subscribe(new Consumer<HttpResult<LoginBean>>() {
                @Override
                public void accept(HttpResult<LoginBean> httpResult) throws Exception {
                    mView.hideLoading();
                    mView.showToast(httpResult.getMessage());
                    if (httpResult.getStatus() == 0) {
                        mView.onLoginSuccess();
                    }
                }
            }, new MHConsumer() {
                @Override
                public void onError(String error) {
                    mView.hideLoading();
                    mView.showToast(error);
                }
            });
    }

    @Override
    public void getLoginInfo() {
        mView.showLoading("登录中...");

        model.getLoginInfo().compose(RxScheduler.Flo_io_main())
            .as(mView.bindAutoDispose())
            .subscribe(new Consumer<HttpResult<LoginBean>>() {
                @Override
                public void accept(HttpResult<LoginBean> httpResult) throws Exception {
                    mView.hideLoading();
                    mView.showToast(httpResult.getMessage());
                    if (httpResult.getStatus() == 0) {
                        mView.onLoginSuccess();
                    }
                }
            }, new MHConsumer() {
                @Override
                public void onError(String error) {
                    mView.hideLoading();
                    mView.showToast(error);
                }
            });
    }
}
